package com.ktwlsoft.archivemanage.domain.business.businessService.problemReply;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.problemReply.ProblemReplyRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.problemReply.ProblemReply;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import com.ktwlsoft.archivemanage.service.utils.SnowFlake;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Optional;

/**
 * 一问一答
 *
 * @author Name on 2023/9/19 10:09
 * @version 1.0
 */
@Component
@RequiredArgsConstructor
public class ProblemReplyService {

    private final ProblemReplyRepositoryService service;

    /**
      * 一问一答列表 on 2023/9/19 10:19
      *
      * @param keyWords keyWords
      * @param state state
      * @return Result<?>
      */
    public Result<?> findPage(int pn,int pageSize,String keyWords,Integer state){
        IPage<ProblemReply> page = service.lambdaQuery()
                .like(StrUtil.isNotBlank(keyWords),ProblemReply::getProblem,keyWords)
                .like(StrUtil.isNotBlank(keyWords),ProblemReply::getQuestioner,keyWords)
                .like(StrUtil.isNotBlank(keyWords),ProblemReply::getReply,keyWords)
                .eq(ObjectUtil.isNotEmpty(state),ProblemReply::getState,state)
                .orderByDesc(ProblemReply::getCreatedAt)
                .page(new Page<>(pn,pageSize));
        return Result.ok(page);
    }

    /**
      * 一问一答发布列表 on 2023/9/19 10:19
      *
      * @return Result<?>
      */
    public Result<?> releaseFindPage(int pn,int pageSize){
        IPage<ProblemReply> page = service.lambdaQuery()
                .eq(ProblemReply::getState, 1)
                .orderByDesc(ProblemReply::getReleaseTime)
                .page(new Page<>(pn,pageSize));
        return Result.ok(page);
    }

    /**
      * 我的提问 on 2023/9/19 10:19
      *
      * @return Result<?>
      */
    public Result<?> myProblem(int pn,int pageSize){
        Account account = Optional.ofNullable(UserPrincipal.getCurrentUser()).orElse(new Account());
        IPage<ProblemReply> page = service.lambdaQuery()
                .eq(ProblemReply::getCreatorId, account.getId())
                .orderByDesc(ProblemReply::getCreatedAt)
                .page(new Page<>(pn,pageSize));
        return Result.ok(page);
    }

    /**
      * 提问 on 2023/9/19 11:25
      *
      * @param problemReply problemReply
      * @return Result<?>
      */
    public Result<?> problem(ProblemReply problemReply){
        Account account = Optional.ofNullable(UserPrincipal.getCurrentUser()).orElse(new Account());
        problemReply.setId(String.valueOf(SnowFlake.getNextId()))
                .setQuestionerId(String.valueOf(account.getId()))
                .setQuestioner(account.getRealName())
                .setQuestionTime(DateUtil.date())
                .setState(0)
                .setDataState(1)
                .setCreatorName(account.getRealName())
                .setCreatorId(String.valueOf(account.getId()))
                .setCreatedAt(DateUtil.date())
                .setUpdateName(account.getRealName())
                .setUpdateId(String.valueOf(account.getId()))
                .setUpdateAt(DateUtil.date());
        service.save(problemReply);
        return Result.ok(problemReply.getId());
    }

    /**
      * 修改 on 2023/9/19 11:25
      *
      * @param problemReply problemReply
      * @return Result<?>
      */
    public Result<?> update(ProblemReply problemReply){
        Account account = Optional.ofNullable(UserPrincipal.getCurrentUser()).orElse(new Account());
        Date date = problemReply.getState().equals(1)?DateUtil.date():null;
        problemReply.setDataState(1)
                .setRecoveryTime(DateUtil.date())
                .setRespondent(account.getRealName())
                .setRespondentId(String.valueOf(account.getId()))
                .setReleaseTime(Optional.ofNullable(problemReply.getReleaseTime()).orElse(date))
                .setUpdateName(account.getRealName())
                .setUpdateId(String.valueOf(account.getId()))
                .setUpdateAt(DateUtil.date());
        service.updateById(problemReply);
        return Result.ok(problemReply.getId());
    }
}